package com.tuananh.radnom.persist.imp.xml;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.xml.transform.stream.StreamSource;

import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;
import org.springframework.oxm.XmlMappingException;

import com.tuananh.radnom.model.AbstractConfiguration;
import com.tuananh.radnom.model.NetElementConfiguration;
import com.tuananh.radnom.model.discovery.DiscoveryConfiguration;
import com.tuananh.radnom.model.event.EventConfiguration;
import com.tuananh.radnom.model.netpoller.NetPollerConfiguration;
import com.tuananh.radnom.model.netpoller.PollingConfiguration;
import com.tuananh.radnom.model.netservice.NetServiceConfiguration;
import com.tuananh.radnom.model.thresholding.ThresholderConfiguration;
import com.tuananh.radnom.model.userinfo.UserConfiguration;
import com.tuananh.radnom.persist.ConfigLoader;

public class XmlConfigHolder {
	public static NetElementConfiguration neConfig;
	public static NetServiceConfiguration netServiceConfig;
	public static NetPollerConfiguration netPollerConfig;
	public static EventConfiguration eventConfig;
	public static ThresholderConfiguration thresholderConfig;
	public static PollingConfiguration pollingConfig;
	public static UserConfiguration userConfig;
	public static DiscoveryConfiguration discoverConfig;

	ConfigLoader loader = new ConfigLoader();

	public ConfigLoader getLoader() {
		return loader;
	}

	public void setLoader(ConfigLoader loader) {
		this.loader = loader;
	}

	private boolean loadConfig(AbstractConfiguration config) {

		return true;
	}

	public XmlConfigHolder() {
		try {

			neConfig = (NetElementConfiguration) loader.getUnmarshaller()
					.unmarshal(new StreamSource(new FileInputStream(NetElementConfiguration.path)));
			netServiceConfig = (NetServiceConfiguration) loader
					.getUnmarshaller().unmarshal(
							new StreamSource(NetServiceConfiguration.path));
			pollingConfig = (PollingConfiguration) loader.getUnmarshaller()
					.unmarshal(new StreamSource(PollingConfiguration.path));

			netPollerConfig = (NetPollerConfiguration) loader.getUnmarshaller()
					.unmarshal(new StreamSource(NetPollerConfiguration.path));
			eventConfig = (EventConfiguration) loader.getUnmarshaller()
					.unmarshal(new StreamSource(EventConfiguration.path));
			thresholderConfig = (ThresholderConfiguration) loader
					.getUnmarshaller().unmarshal(
							new StreamSource(ThresholderConfiguration.path));
			System.out.println("ConfigHolder finished");
			userConfig = (UserConfiguration) loader.getUnmarshaller()
					.unmarshal(new StreamSource(UserConfiguration.path));
			discoverConfig = (DiscoveryConfiguration) loader.getUnmarshaller()
					.unmarshal(new StreamSource(DiscoveryConfiguration.path));

		} catch (XmlMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public NetElementConfiguration getNeConfig() {
		return neConfig;
	}

	public void setNeConfig(NetElementConfiguration neConfig) {
		neConfig = neConfig;
	}

	public NetServiceConfiguration getNetServiceConfig() {
		return netServiceConfig;
	}

	public void setNetServiceConfig(NetServiceConfiguration netServiceConfig) {
		netServiceConfig = netServiceConfig;
	}

	public NetPollerConfiguration getNetPollerConfig() {
		return netPollerConfig;
	}

	public void setNetPollerConfig(NetPollerConfiguration netPollerConfig) {
		netPollerConfig = netPollerConfig;
	}

	public EventConfiguration getEventConfig() {
		return eventConfig;
	}

	public void setEventConfig(EventConfiguration eventConfig) {
		eventConfig = eventConfig;
	}

	public PollingConfiguration getPollingConfig() {
		return pollingConfig;
	}

	public void setPollingConfig(PollingConfiguration pollingConfig) {
		XmlConfigHolder.pollingConfig = pollingConfig;
	}
}
